﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using Common.Util;

namespace DataCenter.Provider
{
    public class WarrantyMasterProvider : GenericProvider, IWarrantyMasterProvider
    {
        public bool Add(WarrantyMaster wSms)
        {
            try
            {
                repository.Add<WarrantyMaster>(wSms);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public List<WarrantyMaster> Search(string serial = null, string model = null, bool? transferFlag = null)
        {
            var querry = repository.GetQuery<WarrantyMaster>();
            if (!serial.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.SerialNo.ToUpper().Contains(serial.ToUpper()));
            }

            if (!model.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.Model.ToUpper().Contains(model.ToUpper()));
            }

            if (transferFlag.HasValue)
            {
                string flag = transferFlag.Value.ToBooleanString();
                if (transferFlag.Value) querry = querry.Where(x => x.SoTransferFlag.ToUpper() == flag);
                else querry = querry.Where(x => x.SoTransferFlag == null || x.SoTransferFlag.Length == 0 || x.SoTransferFlag.ToUpper() == flag);
            }

            return querry.ToList();
        }


        public WarrantyMaster Retrieve(string serial, string model, string shopcode)
        {
            var querry = repository.GetQuery<WarrantyMaster>();
            if (!serial.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.SerialNo.ToUpper() == serial.ToUpper());
            }

            if (!model.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.Model.ToUpper() == model.ToUpper());
            }

            if (!shopcode.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.ShopCode.ToUpper() == shopcode.ToUpper());
            }
            return querry.FirstOrDefault();
        }

        public bool Update(WarrantyMaster wtMaster)
        {
            var updateRecord = Retrieve(wtMaster.SerialNo, wtMaster.Model, wtMaster.ShopCode);
            try
            {
                updateRecord.Amt = wtMaster.Amt;
                updateRecord.CloseFlag = wtMaster.CloseFlag;
                updateRecord.ClosePeriod = wtMaster.ClosePeriod;
                updateRecord.CloseUser = wtMaster.CloseUser;
                updateRecord.CreateDate = wtMaster.CreateDate;
                updateRecord.EndDate = wtMaster.EndDate;
                updateRecord.EndUserCell = wtMaster.EndUserCell;
                updateRecord.IncentiveCfmDate = wtMaster.IncentiveCfmDate;
                updateRecord.IncentiveCfmFlag = wtMaster.IncentiveCfmFlag;
                updateRecord.IncentiveCfmPeriod = wtMaster.IncentiveCfmPeriod;
                updateRecord.IncentiveCfmUser = wtMaster.IncentiveCfmUser;
                updateRecord.LastUpdateDate = wtMaster.LastUpdateDate;
                //updateRecord.Model = wtMaster.Model;
                updateRecord.Point = wtMaster.Point;
                updateRecord.SellinDate = wtMaster.SellinDate;
                updateRecord.SelloutDate = wtMaster.SelloutDate;
                updateRecord.SelloutRespMsg = wtMaster.SelloutRespMsg;
                updateRecord.SelloutRespType = wtMaster.SelloutRespType;
                updateRecord.SelloutTime = wtMaster.SelloutTime;
                //updateRecord.SerialNo = wtMaster.SerialNo;
                updateRecord.ShopCell = wtMaster.ShopCell;
                //updateRecord.ShopCode = wtMaster.ShopCode;
                updateRecord.SmsYn = wtMaster.SmsYn;
                updateRecord.StartDate = wtMaster.StartDate;
                updateRecord.Suffix = wtMaster.Suffix;
                updateRecord.TransferFlag = wtMaster.TransferFlag;
                updateRecord.SoTransferFlag = wtMaster.TransferFlag;
                if (updateRecord.SoTransferFlag.IsYes()) updateRecord.SoTransferDate = DateTime.Now;
                if (updateRecord.TransferFlag.IsYes()) updateRecord.TransferDate = DateTime.Now;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
